package com.huawei.codearts.controller;

import com.huawei.codearts.service.EtcdService;
import com.huawei.codearts.tools.utils.ResponseUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value = "etcd")
@RequiredArgsConstructor
public class EtcdController {

    private final EtcdService etcdService;

//    @RequestMapping(value = "put")
    @PostMapping(value = "put")
    public String put(@RequestParam Map<String, Object> params) {
        // @RequestBody 接收json参数
        // @RequestParam 接收get或post参数, 如果get和post同时有参数并且参数名称相同, 则get参数会覆盖post参数
        System.out.println(params);
        System.out.println(params.get("name"));
        System.out.println(params.get("value"));

        String name = (String)params.get("name");
        String value = (String)params.get("value");

        String result = null;
        try {
            result = ResponseUtils.response(etcdService.put(name, value));
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

//    @RequestMapping(value = "get")
    @GetMapping(value = "get")
    public String get(@RequestParam Map<String, Object> params) {
        // @RequestBody 接收json参数
        // @RequestParam 接收get或post参数, 如果get和post同时有参数并且参数名称相同, 则get参数会覆盖post参数
        System.out.println(params);
        System.out.println(params.get("name"));

        String name = (String)params.get("name");

        String result = null;
        try {
            result = ResponseUtils.response(etcdService.get(name));
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping(value = "delete")
    public String delete(@RequestParam Map<String, Object> params) {
        // @RequestBody 接收json参数
        // @RequestParam 接收get或post参数, 如果get和post同时有参数并且参数名称相同, 则get参数会覆盖post参数
        System.out.println(params);
        System.out.println(params.get("name"));

        String name = (String)params.get("name");

        String result = null;
        try {
            result = ResponseUtils.response(etcdService.delete(name));
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping(value = "watch")
    public String watch(@RequestParam Map<String, Object> params) {
        String result = null;
        try {
            result = ResponseUtils.response(etcdService.watch());
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

    @RequestMapping(value = "lock")
    public String lock(@RequestParam Map<String, Object> params) {
        String result = null;
        try {
            result = ResponseUtils.response(etcdService.lock());
        } catch(Exception e) {
            result = ResponseUtils.logException(e);
        }
        return result;
    }

}
